#include <tsubamegaeshi-base/agent.h>

#include <iostream>

Agent::Agent(Behavior::shared_ptr b, Connection::shared_ptr c)
  : conn_(c), behavior_(b), loop_(true)
{
}

Agent::Agent(Behavior::shared_ptr b, const std::string& h, const int p)
  : conn_(Connection::shared_ptr(new Connection(h, p))),
    behavior_(b), loop_(true)
{
}

Agent::~Agent()
{
}

void  
Agent::run()
{
  conn_->put_message(behavior_->init());
    
  std::string msg;
  while (loop_ && conn_->get_message(msg)) {  
    conn_->put_message(behavior_->think(msg));
  }
}

void
Agent::done()
{
  conn_->close();
  std::cout << "closed connection to " << conn_->host() << ":" << conn_->port() << std::endl;
}

void
Agent::stop()
{
  loop_ = false;
}
